package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.mapper.entity.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface BadgeManageMapper extends BaseMapper<Badge> {

    @Select("SELECT DISTINCT attribute_id, attribute_name FROM badge_view " +
            "ORDER BY attribute_id ASC")
    List<BadgeView> getBadgeTabs();

    @Select("SELECT * FROM badge_view WHERE attribute_id = #{attributeId}")
    List<BadgeView> getBadgeList(BadgeView badge);

    @Select("SELECT * FROM badge_view WHERE type = 1")
    List<BadgeView> getGrowthBadgeList();

    @Select("SELECT * FROM badge_view WHERE type = 2")
    List<BadgeView> getAchievementBadgeList();

    @Select("SELECT * FROM badge_view WHERE type = 3")
    List<BadgeView> getSpecialBadgeList();

    @Delete("DELETE FROM `badge` WHERE badge_id = #{badgeId}")
    void deleteBadgeById(BadgeView request);

    @Delete("<script>" +
            "DELETE FROM badge" +
            " WHERE badge_id IN" +
            " <foreach collection='list' index='index' open='(' separator=',' close=')' item='badgeList' >" +
            "   #{badgeList.badgeId}" +
            " </foreach>" +
            "</script>")
    void batchDeleteBadge(List<Badge> badgeList);


    @Update("UPDATE `badge` SET name = #{name},description = #{description},type = #{type},task_id = #{taskId}," +
            "attribute_id = #{attributeId} ,attribute_value = #{attributeValue} ,badge_number = #{badgeNumber} " +
            "WHERE badge_id = #{badgeId}")
    void updateBadgeList(Badge newBadge);

    @Select("SELECT * FROM badge WHERE badge_id = #{badgeId}")
    Badge selectImageUrl(int badgeId);

    @Select("<script>" +
            "SELECT * " +
            " FROM `badge_view` " +
            "<where> " +
            "  <if test='newBadge.type != null and newBadge.type != \"\" '>" +
            "     AND type = #{newBadge.type} " +
            "  </if>" +
            "  <if test='newBadge.badgeName != null and newBadge.badgeName != \"\" '>" +
            "     AND badge_name = #{newBadge.badgeName} " +
            "  </if>" +
            "  <if test='newBadge.attributeName != null and newBadge.attributeName != \"\" '>" +
            "     AND attribute_name = #{newBadge.attributeName} " +
            "  </if>" +
            "  <if test='newBadge.taskType != 0'>" +  // 可以直接检查是否不等于0
            "     AND task_type = #{newBadge.taskType} " +
            "  </if>" +
            "</where>" +
            "</script>")
    List<BadgeView> getSelectBadge(@Param("newBadge") BadgeView newBadge);

    @Select("SELECT * FROM badge WHERE badge_id = #{badgeId}")
    Badge selectBadgeById(int badgeId);

    @Update("UPDATE `badge` SET image_url = #{imageUrl}" +
            "WHERE badge_id = #{badgeId}")
    void placeBadgePic(Badge currentBadge);

    @Select("SELECT * FROM student_badge_view")
    List<StudentBadgeView> getBadgeLog();

}
